﻿$(document).ready(function () {
    page.init();
});

var page = {
    url: "/handler/auction.ashx?action=",
    type: 0,
    popU: null,
    lastInterval: null,

    init: function () {
        $(".top-bar .right li:eq(1)").addClass("current");
        page.getList();

        window.setInterval(page.fresh, 10000); //10秒更新一次
    },

    fresh: function () {
        $.get(page.url + "list", { r: Math.random() }, function (data) {
            page.lastSeconds(data.lastSecond);
            page.list = data.list;
            page.filter($(".tabs dd.active").index());
        }, "json");
    },

    filter: function (type) {
        if (type != undefined)
            page.type = type;
        switch (type) {
            case 2:
                var list = [];
                for (var i = 0; i < page.list.length; i++) {
                    if (page.list[i].BidAmount + 100 <= page.currentUser.available || page.list[i].Bidder == page.currentUser.userId) {
                        list.push(page.list[i]);
                    }
                }
                $("#result").html(TrimPath.processDOMTemplate("auctionlist", { list: list }));
                break;
            case 1:
                var watch = $.cookie("watch");
                if (watch == null) {
                    $("#result").empty();
                    return;
                }
                var list = [];
                for (var i = 0; i < page.list.length; i++) {
                    if (watch.indexOf("," + page.list[i].Id + ",") > -1) {
                        list.push(page.list[i]);
                    }
                }
                $("#result").html(TrimPath.processDOMTemplate("auctionlist", { list: list }));
                break;
            case 0:
            default:
                $("#result").html(TrimPath.processDOMTemplate("auctionlist", page));
                break;
        }
    },

    lastSeconds: function (second) {
        window.clearInterval(page.lastInterval);
        page.lastInterval = window.setInterval(function () {
            second -= 1;
            page.lastSeconds(second);
        }, 1000);

        if (second > 0) {
            $("#secondPanel").css({ left: $(window).width() / 2 + 250 });
            var day = parseInt(second / 86400);
            var hour = parseInt((second % 86400) / 3600);
            var min = parseInt((second % 3600) / 60);
            var sec = parseInt(second % 60);

            var str = [];
            if (day > 0)
                str.push(day + "天");
            if (hour > 0)
                str.push(hour + "小时");
            if (min > 0)
                str.push(min + "分");
            if (sec > 0)
                str.push(sec + "秒");

            $("#secondPanel label").html(str.join(''));
        }
        else {
            page.getList();
            $("#secondPanel").hide();
        }
    },

    getList: function () {
        $.get(page.url + "list", { r: Math.random() }, function (data) {
            page.lastSeconds(data.lastSecond);
            page.list = data.list;
            if (data.list.length == 0) {
                $(".tabs").hide();
            }
            $("#result").html(TrimPath.processDOMTemplate("auctionlist", data));

            $(".tabs dd").unbind("click").click("click", function () {
                $(this).parent().find("dd").removeClass("active");
                $(this).addClass("active");
                page.filter($(this).index());
            });
        }, "json");
    },

    showBid: function (auctionId, auctionItemId) {
        var html = [];
        html.push("<div id='panelItem'></div>");

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "竞拍详情",
            contents: html.join(""),
            w: 600,
            y: $(window).scrollTop() + 30,
            drag: true
        });
        page.popU.showPop();

        var item;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == auctionItemId) {
                item = page.list[i];
                break;
            }
        }

        $("#panelItem").html(TrimPath.processDOMTemplate("auction_detail", item));
        $(".currentAvailable").html(page.currentUser.available);
        $("#cAmount").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");
    },

    getBid: function (auctionId, itemId) {
        $.post(page.url + "queryBid", { auctionId: auctionId, auctionItemId: itemId }, function (data) {
            $("#bilist").html(TrimPath.processDOMTemplate("bidlist", data));
            for (var i = 1; i < data.list.length; i++) {
                if (data.list[i].UserId == page.currentUser.userId) {
                    $("#btnCancel").show();
                    break;
                }
            }
        }, "json");
    },

    cancelBid: function (auctionId, itemId) {
        $.post(page.url + "cancelBid", { auctionId: auctionId, auctionItemId: itemId }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            $("#btnCancel").remove();
            $("#bilist").html(TrimPath.processDOMTemplate("bidlist", data));
        }, "json");
    },

    allIn: function () {
        $("#cAmount").val(page.currentUser.available);
    },

    bid: function (auctionId, itemId) {
        var amount = $("#cAmount").val();
        var item;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == itemId) {
                item = page.list[i];
                break;
            }
        }
        if ($.trim(amount) == "" || parseInt(amount) > page.currentUser.available || parseInt(amount) < item.BidAmount + 100) {
            alert("竞拍素币必须大于当前最高竞拍100素币且少于您当前可用素币");
            $("#cAmount").focus();
            return false;
        }
        if (page.process) {
            alert("操作处理中，请稍后再试");
            return false;
        }

        page.process = true;
        $.post(page.url + "bid", { auctionId: auctionId, auctionItemId: itemId, amount: amount }, function (data) {
            $("#cAmount").val('');
            page.process = false;
            if (data.error) {
                alert(data.error);
                page.getBid(auctionId, itemId);
                return false;
            }
            $("#bilist").html(TrimPath.processDOMTemplate("bidlist", data));
        }, "json");
    },

    watch: function (id, obj) {
        var watch = $.cookie("watch");
        if (watch == null) {
            watch = ",";
        }
        if (watch.indexOf("," + id + ",") > -1)
            watch = watch.replace("," + id + ",", ",");
        else
            watch += id + ",";
        $.cookie("watch", watch);

        if ($(obj).val() == "关注") {
            $(obj).addClass("success");
            $(obj).val("取消关注");
        } else {
            $(obj).removeClass("success");
            $(obj).val("关注");
        }
    },

    checkWatch: function (id, type) {
        var watch = $.cookie("watch");
        if (watch == null)
            return type == 1 ? "" : "";

        if (watch.indexOf("," + id + ",") > -1)
            return type == 1 ? "success" : "取消";
    }
};

